﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;

using MSDIS.Configuration.Parameters;
using MSDIS.Configuration;
using MSDIS.Web.Injection;

namespace MSDIS.Web.Controllers
{
    public class EndpointsController : ApiController
    {
        private IEnumerable<EndpointParameter> _parameters;
        private EndpointParameter _parameter;
        private IVariable _variable;

        public IEnumerable<EndpointParameter> Get()
        {
            _variable = IoC.ConfigurationManager.GetVariableByName("Endpoints");
            _parameters = (IEnumerable<EndpointParameter>) _variable.Value;
            return _parameters;
        }

        public EndpointParameter Put(EndpointParameter endpoint)
        {
            _parameter = endpoint;

            Update(endpoint);

            return _parameter;
        }

        private void Update(EndpointParameter endpoint)
        {
            _variable = IoC.ConfigurationManager.GetVariableByName("Endpoints");
            _parameters = (IEnumerable<EndpointParameter>)_variable.Value;

            List<EndpointParameter> list = _parameters.ToList();

            int maxId = list.Max(e => e.Id);

            if (endpoint.Id < 0)
            {
                endpoint.Id = maxId + 1;
                list.Add(endpoint);
            }
            else
            {
                list[list.FindIndex(e => e.Id == endpoint.Id)] = endpoint;
            }

            _variable.Value = list.AsEnumerable();
            IoC.ConfigurationManager.UpdateVariable(_variable);
        }

        public int Delete(int id)
        {
            _variable = IoC.ConfigurationManager.GetVariableByName("Endpoints");
            _parameters = (IEnumerable<EndpointParameter>)_variable.Value;

            List<EndpointParameter> list = _parameters.ToList();

            list.Remove(list.First(e => e.Id == id));

            _variable.Value = list.AsEnumerable();
            IoC.ConfigurationManager.UpdateVariable(_variable);

            return id;
        }
    }

    public class FusionController : ApiController
    {
        private FusionParameter _parameter;
        private IVariable _variable;

        public FusionParameter Get()
        {
            _variable = IoC.ConfigurationManager.GetVariableByName("Fusion");
            return (FusionParameter) _variable.Value;
        }

        public FusionParameter Put(FusionParameter fusion)
        {
            _parameter = fusion;
            return _parameter;
        }

        public void Post(FusionParameter fusion)
        {
            _parameter = fusion;
            _variable = IoC.ConfigurationManager.GetVariableByName("Fusion");
            _variable.Value = _parameter;
            IoC.ConfigurationManager.UpdateVariable(_variable);
        }
    }

    public class SecurityController : ApiController
    {
        private SecurityParameter _parameter;
        private IVariable _variable;

        public SecurityParameter Get()
        {
            _variable = IoC.ConfigurationManager.GetVariableByName("Security");
            return (SecurityParameter)_variable.Value;
        }
        public SecurityParameter Put(SecurityParameter security)
        {
            _parameter = security;
            return _parameter;
        }

        public void Post(SecurityParameter security)
        {
            _parameter = security;
            _variable = IoC.ConfigurationManager.GetVariableByName("Security");
            _variable.Value = _parameter;
            IoC.ConfigurationManager.UpdateVariable(_variable);
        }
    }

    public class StatisticsController : ApiController
    {
        
    }

    public class ServicesController : ApiController
    {
        private IEnumerable<RemoteService> _parameters;
        private RemoteService _parameter;
        private IVariable _variable;

        public IEnumerable<RemoteService> Get()
        {
            _variable = IoC.ConfigurationManager.GetVariableByName("Services");
            _parameters = (IEnumerable<RemoteService>)_variable.Value;
            return _parameters;
        }

        public RemoteService Put(RemoteService service)
        {
            _parameter = service;
            return _parameter;
        }

        public RemoteService Post(RemoteService service)
        {
            return null;
        }
    }
}
